package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sl.mapper.AnswerMapper;
import com.sl.pojo.Answer;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AnswerService {
@Resource
    private AnswerMapper answerMapper;

    //查：所有   问卷
    public Page<Answer> findAll(Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<Answer> companyChecks = answerMapper.selectAll();
        return (Page<Answer>)companyChecks;
    }
    //查：单个  id
    public Answer findById(Integer id){
        return answerMapper.selectByPrimaryKey(id);
    }
    //查：条件   公司
    public List<Answer> findList(Map<String,String> searchMap){
        Example example = createExample(searchMap);
        List<Answer> companies = answerMapper.selectByExample(example);
        return companies;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, String> searchMap){
        Example example=new Example(Answer.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            //公司名称ID
            if(searchMap.get("enterpriseId")!=null ){
                criteria.andEqualTo("enterpriseId",searchMap.get("enterpriseId"));
            }
            //检查人员ID
            if(searchMap.get("userId")!=null ){
                criteria.andEqualTo("userId",searchMap.get("userId"));
            }
            //试卷ID
            if(searchMap.get("paperId")!=null ){
                criteria.andEqualTo("paperId",searchMap.get("paperId"));
            }
            //状态。待检测，检测中，已完成
            if(searchMap.get("status")!=null ){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            //positioning
            if(searchMap.get("positioning")!=null ){
                criteria.andLike("positioning", "%"+searchMap.get("positioning")+"%");
            }
            //road
            if(searchMap.get("road")!=null ){
                criteria.andLike("road", "%"+searchMap.get("road")+"%");
            }
            //answer答案
            if(searchMap.get("answer")!=null ){
                criteria.andLike("answer", "%"+searchMap.get("answer")+"%");
            }
            //add时间。。创建时间
            if(searchMap.get("startTime")!=null ){
                System.out.println("检查时间>>>>" + searchMap.get("startTime"));
                criteria.andLike("startTime","%"+searchMap.get("startTime")+"%");
            }
            //end提交时间
            if(searchMap.get("endTime")!=null ){
                criteria.andLike("endTime","%"+searchMap.get("endTime")+"%");
            }
        }
        return example;
    }
    //增：一个问卷
    public void addAnswer(Answer answer){
        answerMapper.insertSelective(answer);}
    //更新：问卷信息
    public void updateAnswer(Answer answer){
        answerMapper.updateByPrimaryKeySelective(answer);}

}
